package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.outto.core.*;
import com.ruoyi.system.outto.dto.HhbDto;
import com.ruoyi.system.outto.dto.HhbProductDto;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.IHHBAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class HHBImpl implements IHHBAPI {
    @Resource
    private InfoImgMapper infoImgMapper;
    @Autowired
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;

    @Override
    public AjaxResult addAndEditAndDelGame(AccountInfo accountInfo, String doType, Long userId, SalePartner sa) {


        Map<String, Object> hhbDto = params(accountInfo, doType,userId,sa);
        String path = "/api/pushProduct";
//        Gson gson = new Gson();
//        String json = gson.toJson(hhbDto);
        String json = JSONObject.toJSONString(hhbDto);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("data", json);
        hashMap.put("sign", DigestUtils.md5Hex(json + sa.getSaleKey()));
        System.out.println(hashMap);
        log.info("换号吧新增请求参数={}", hashMap);

        String response = HttpUtils.doPostForm(OutUrlConstant.HHBURL + path,hashMap,null);
        log.info("{}", response);
        JSONObject resultJson = JSONObject.parseObject(response);

        Map<String, Object> result = new HashMap<>();

        if (StringUtils.isEmpty(resultJson)){
            result.put("code",500);
            result.put("req",JSONObject.toJSONString(hashMap));
            result.put("res",response);
            result.put("msg","图片存有特殊字符串,换号吧不允许新增或者编辑");
            return AjaxResult.success(result);
        }
        String msg = resultJson.getString("msg");


        if (!resultJson.getBoolean("succ")) {
            result.put("code",500);
        }else {
            result.put("code",200);
        }

        result.put("req",JSONObject.toJSONString(hashMap));
        result.put("res",response);
        result.put("msg",msg);
        return AjaxResult.success(result);
    }

    private Map<String, Object> params(AccountInfo accountInfo, String doType,Long userId,SalePartner sa) {

        HashMap<String, Object> params = new HashMap<>();
        HhbDto hhbDto = new HhbDto();
        hhbDto.setAppid(sa.getOther());
        params.put("appid",sa.getOther());
        hhbDto.setAct(doType);
        params.put("act",doType);
        HhbProductDto hhbProductDto = new HhbProductDto();
        HashMap<String, Object> params2 = new HashMap<>();

        hhbProductDto.setId(accountInfo.getId());
        params2.put("id",accountInfo.getId());
        hhbProductDto.setTitle(accountInfo.getDes());
        params2.put("title", descTitle+"_" + accountInfo.getId()+ accountInfo.getLevel() + "  " + accountInfo.getDes());
        BigDecimal bigDecimal = commonApi.addPrice(accountInfo.getPrice(), sa.getSeleep());
        commonApi.setPrice(bigDecimal,accountInfo.getId(),sa);
        hhbProductDto.setPrice(bigDecimal);
        params2.put("price",bigDecimal);

        //图片
        StringBuilder imagesSb = new StringBuilder();
        InfoImg infoImg = new InfoImg();
        infoImg.setInfoId(accountInfo.getId());
        infoImg.setType(Constants.STR_1);
        List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);
        if (CollectionUtils.isEmpty(infoImgs)) {
            hhbProductDto.setPic(sa.getTwoOther());
            params2.put("pic",sa.getTwoOther());
        } else {
            for (InfoImg img : infoImgs) {
                imagesSb.append(img.getImgUrl()).append(",");
            }
            String images = imagesSb.toString();
            hhbProductDto.setPic(images);
            params2.put("pic",images);
        }

        if (StringUtils.isEmpty(hhbProductDto.getPic())){
            hhbProductDto.setPic("---");
            params2.put("pic","---");
        }
        String hhbArea = RegionalCovert.toHHBserver(accountInfo.getType(),accountInfo.getRegional());
        if (StringUtils.isNull(hhbArea)) {
            throw new GlobalException("游戏大区未找到"+hhbArea);
        }
        hhbProductDto.setArea(hhbArea);
        params2.put("area",hhbArea);
        Integer type = RegionalCovert.typeToHHB(accountInfo.getType());
        if (type == null) {
            log.error("游戏未找到{}", accountInfo.getType());
            throw new GlobalException("游戏未找到"+type);
        }

        String hhBserver = RegionalCovert.toHHBserver(accountInfo.getType(),accountInfo.getRegional());
        if (hhBserver == null) {
            log.error("游戏服务器未找到{}", accountInfo.getRegional());
            throw new GlobalException("游戏未找到"+accountInfo.getRegional());
        }

        hhbProductDto.setGame(type);
        params2.put("game",type);

        hhbProductDto.setServer(null);
        params2.put("server",null);

        hhbProductDto.setRole("11,12");
        params2.put("role","11,12");

        hhbProductDto.setBface(0);
        params2.put("bface",0);

        if ("王者荣耀".equals(accountInfo.getType())) {
            hhbProductDto.setParam1(accountInfo.getHeroes());

            hhbProductDto.setParam2(accountInfo.getSkins());
            params2.put("param2",accountInfo.getSkins());
            params2.put("param1",accountInfo.getHeroes());

        }
        if ("和平精英".equals(accountInfo.getType())) {
            hhbProductDto.setParam1(accountInfo.getSuit());
            hhbProductDto.setParam2(accountInfo.getGuns());
            params2.put("param2",accountInfo.getGuns());
            params2.put("param1",accountInfo.getSuit());
        }
        if ("英雄联盟".equals(accountInfo.getType())) {
            hhbProductDto.setParam1(accountInfo.getSuit());
            hhbProductDto.setParam2(accountInfo.getGuns());
            params2.put("param2",accountInfo.getSkins());
            params2.put("param1",accountInfo.getHeroes());
        }

        if ("CF端游".equals(accountInfo.getType())) {
            params2.put("param","409");
        }


        hhbProductDto.setCanreal(0);
        if (Objects.equals(LCConstant.haveTwo,accountInfo.getLevel())){
            params2.put("canreal",1);
        }

        if (Objects.equals(LCConstant.bakFace,accountInfo.getLevel())){
            params2.put("canreal",2);
        }

        hhbDto.setProduct(hhbProductDto);
        params.put("product",params2);

        return params;
    }
}
